package com.itheima.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author lvFF
 * @Date 2022/04/26/下午 4:26;
 * Description
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Override
    public void remove(Long id) {
        /**
         * //查询是否关联菜品
        LambdaQueryWrapper<Dish> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(Dish::getCategoryId, id);
        int count1 = dishService.count(lambdaQueryWrapper1);
        //判断
        if (count1>0){
            //有关联,抛出异常
            throw new CustomException("当下分类下还有菜");
        }
        //查询是否关联套餐
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(lambdaQueryWrapper2);
        if (count2>0){
            //有关联,抛出异常
            throw new CustomException("当下分类下还有套餐");
        }*/
        QueryWrapper<Dish> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("category_id",id);
        int count1 = dishService.count(queryWrapper1);
        if (count1>0){
            throw  new CustomException("当下菜品下还有菜");
        }
        QueryWrapper<Setmeal> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("category_id",id);
        int count2 = setmealService.count(queryWrapper2);
        if (count2>0){
            throw  new CustomException("当下套餐下还有套餐");
        }
        this.removeById(id);
    }
}
